class Ddapi {
  
    //钉钉图片显示
    previewImage(urls, thisUrl) {
        dd.biz.util.previewImage({
            urls: urls, //图片地址列表
            current: thisUrl, //当前显示的图片链接
            onSuccess: function(result) {
                /**/
            },
            onFail: function(err) {}
        })
    }
  
    //钉钉alert项目提示框
    alert(msgtext) {
        return new Promise(function(resolve) {
            dd.device.notification.alert({
                message: msgtext,
                title: "提示", //可传空
                buttonName: "确认",
                onSuccess: function() {},
                onFail: function(err) {}
            });
        })
    }

    //钉钉confirm项目提示框
    confirm(msgtext) {
        return new Promise(function(resolve) {
            dd.device.notification.confirm({
                message: msgtext,
                title: "提示",
                buttonLabels: ['确认', '取消'],
                onSuccess: function(result) {
                    resolve(result.buttonIndex)
                },
                onFail: function(err) {}
            });
        })
    }
  
    //钉钉提示信息
    toast(text, icon) {
        dd.device.notification.toast({
            icon: icon, //icon样式，有success和error，默认为空 0.0.2
            text: text, //提示信息
            duration: 2, //显示持续时间，单位秒，默认按系统规范[android只有两种(<=2s >2s)]
            delay: 0, //延迟显示，单位秒，默认0
            onSuccess: function(result) {},
            onFail: function(err) {}
        });
    }
  
    //获取钉钉个人信息[头像，名称]
    getUserInfo() {
        return new Promise((resolve, reject) => {
            dd.biz.user.get({
                onSuccess: function(info) {
                    resolve(info);
                },
                onFail: function(err) {
                alert(JSON.stringify(err))
                    reject(err);
                }
            })
        })
    }
  
    //打开http跳转
    openLink(url) {
        dd.biz.util.openLink({
            "url": url,
            "enableShare": true
        });
    }

    openApp(agentId){
        return new Promise((resolve, reject) => {
            dd.biz.microApp.openApp({
                agentId: agentId,
                onSuccess : function(result) {
                    resolve(result)
                },
                onFail : function(err) {
                    reject(err)
                }
            })
        })
    }

    uploadImages(maxNum) {
        return new Promise(function(resolve, reject) {
            dd.biz.util.uploadImage({
                compression: false, //(是否压缩，默认为true)
                multiple: true, //是否多选，默认false
                max: maxNum, //最多可选个数
            onSuccess: function(result) {
                resolve(result)
            },
            onFail: function(err) {
                reject("取消上传图片");
            }
        })
        })
    }
  
    //钉钉标题
    setTitle(title) {
        dd.biz.navigation.setTitle({
            title: title, //控制标题文本，空字符串表示显示默认文本
            onSuccess: function(result) {},
            onFail: function(err) {}
        });
    }
  
  
    //loading
    showLoading(title) {
        dd.device.notification.showPreloader({
            text: title, //loading显示的字符，空表示不显示文字
            showIcon: true, //是否显示icon，默认true
            onSuccess: function(result) {
                /*{}*/
            },
            onFail: function(err) {}
        })
    }
  
    //禁用加载
    hidePreloader() {
        dd.device.notification.hidePreloader({
            onSuccess: function(result) {},
            onFail: function(err) {}
        })
    }
  
  }
  
  let ddapi = new Ddapi();
  export default ddapi;